<?php

function FormCheck(){
		$errors = array();
		$first_name = isset($_POST[x_first_name]) ?$_POST[x_first_name]: '';
		$last_name = isset($_POST[x_last_name]) ?$_POST[x_last_name]: '';
		$phone1_a  = isset($_POST[x_phone_1]) ? $_POST[x_phone_1]: '';
		$phone1_b  = isset($_POST[x_phone_2]) ? $_POST[x_phone_2]: '';
		$phone1_c  = isset($_POST[x_phone_3]) ? $_POST[x_phone_3]: '';
		$address = isset($_POST[x_address_1]) ?$_POST[x_address_1]: '';
		$address2 = isset($_POST[x_address_2]) ?$_POST[x_address_2]: '';
		$city = isset($_POST[x_city]) ?$_POST[x_city]: '';
		$state = isset($_POST[state]) ?$_POST[state]: '';
		$state = isset($_POST[x_state]) ?$_POST[x_state]: $state;
		$zip = isset($_POST[x_zip]) ?$_POST[x_zip]: '';
		$card_number = isset($_POST[x_card_num]) ?$_POST[x_card_num]: '';
		$card_expires_m = isset($_POST[x_exp_date_m]) ?$_POST[x_exp_date_m]: '';
		$card_expires_y = isset($_POST[x_exp_date_y]) ?$_POST[x_exp_date_y]: '';
		$promo_code = isset($_POST[promo_code]) ?$_POST[promo_code]: '';
		$altiris_code = isset($_POST[altiris_code]) ?$_POST[altiris_code]: '';
		$discount_code = isset($_POST[discount_code]) ?$_POST[discount_code]: '';
		$total = isset($_POST[x_amount]) ?$_POST[x_amount]: '';
		foreach($_POST as $key =>$entry){
			$search = array('_1', '_2', '_3','x_', '_');
			$replace = array('','','','',' ');
			$field = str_replace($search,$replace, $key); 
			if(trim($entry) == '' && (($key !='x_address_2') && ($key !='altiris_code') && ($key !='promo_code') && ($key !='discount_code')))
				$errors[$key] = "Please enter your $field";
			if(($key == 'x_phone_1' && strlen($entry) != 3) || ($key == 'x_phone_2' && strlen($entry) != 3) || ($key == 'x_phone_3' && strlen($entry) != 4))
				$errors[$key] = "Please enter a valid $field number ";
		}
		
		if(count($errors)<1){
			unset($errors);
			$form_vars = array();		
			$form_vars[x_first_name] = $first_name;
			$form_vars[x_last_name] = $last_name;
			$form_vars[x_address_1] = $address2 != '' ? $address . ' '. $address2: $address;
			$form_vars[x_city] = $city;
			$form_vars[x_state] = $state;
			$form_vars[x_zip] =$zip;
			$form_vars[x_phone] = $phone1_d != '' ? $phone1_a.$phone1_b.$phone1_c.$phone1_d: $phone1_a.$phone1_b.$phone1_c;
	//		$form_vars[x_exp_date] = $card_expires_m . '/' . $card_expires_y;
	//		$form_vars[x_card_num] =$card_number;
			$form_vars[x_exp_date] = "1209";
			$form_vars[x_card_num] = "4242424242424242";
			$form_vars[promo_code] =$promo_code;
			$form_vars[altiris_code] =$altiris_code;
			$form_vars[discount_code] =$discount_code;
			$form_vars[x_amount] =$total;
			return $form_vars;
		}
		else{
			$errors[0] = "ERRORS"; 
			return array_unique($errors);
		}
	}


	//  Simple Funciton that returns a string containing a 
	//  select box with each of the 50 US states and a default value "Select State" 
	//  You can pass in a state name as a string to select a specific State.  i.e. StateSelect('Virginia');
	function StateSelect($selected = 'Select State'){
		$state['default'] = 'Select State';
		$state[AL] = "Alabama";
		$state[AK] = "Alaska";
		$state[AZ] = "Arizona";
		$state[AR] = "Arkansas";
		$state[CA] = "California";
		$state[CO] = "Colorado";
		$state[CT] = "Connecticut";
		$state[DE] = "Delaware";
		$state[FL] = "Florida";
		$state[GA] = "Georgia";
		$state[HI] = "Hawaii";
		$state[ID] = "Idaho";
		$state[IL] = "Illinois";
		$state[IN] = "Indiana";
		$state[IA] = "Iowa";
		$state[KS] = "Kansas";
		$state[KY] = "Kentucky";
		$state[LA] = "Louisiana";
		$state[ME] = "Maine";
		$state[MD] = "Maryland";
		$state[MA] = "Massachusetts";
		$state[MI] = "Michigan";
		$state[MN] = "Minnesota";
		$state[MS] = "Mississippi";
		$state[MO] = "Missouri";
		$state[MT] = "Montana";
		$state[NE] = "Nebraska";
		$state[NV] = "Nevada";
		$state[NH] = "New Hampshire";
		$state[NJ] = "New Jersey";
		$state[NM] = "New Mexico";
		$state[NY] = "New York";
		$state[NC] = "North Carolina";
		$state[ND] = "North Dakota";
		$state[OH] = "Ohio";
		$state[OK] = "Oklahoma";
		$state['OR'] = "Oregon";
		$state[PA] = "Pennsylvania";
		$state[RI] = "Rhode Island";
		$state[SC] = "South Carolina";
		$state[SD] = "South Dakota";
		$state[TN] = "Tennessee";
		$state[TX] = "Texas";
		$state[UT] = "Utah";
		$state[VT] = "Vermont";
		$state[VA] = "Virginia";
		$state[WA] = "Washington";
		$state[WV] = "West Virginia";
		$state[WI] = "Wisconsin";
		$state[WY] = "Wyoming";
		$options = '';
		foreach ($state as $value => $display)
			$options .= $display == $selected ? "\t\t<option value=\"$value\" selected>$display</option>" . PHP_EOL  
															  :	"\t\t<option value=\"$value\">$display</option>" . PHP_EOL;		
		unset($state);
		$stateSelect = "\t<select name=\"x_state\" size=\"1\">".PHP_EOL . $options . "\t</select>" . PHP_EOL;
		return $stateSelect;
	}
		
	function DisplayOrderForm(){
	$payment_url = '/process-order';
	$total = 100.99;
	$payment_form = "<form action=\"".$payment_url."\" method=\"post\" name=\"payment\">" .PHP_EOL;
	$payment_form .= '<input name="type" type="hidden" value="order" />';
	$payment_form .= '	<table class="data" border="0" cellpadding="0" cellspacing="0">'. PHP_EOL;
	$payment_form .= '		<tbody><tr class="roweven">'. PHP_EOL;
	$payment_form .= '			<th colspan="5">Payment</th>'. PHP_EOL;
	$payment_form .= '		</tr>'. PHP_EOL;
	$payment_form .= '	<tr class="roweven">'.PHP_EOL;
	$payment_form .= '				<td rowspan="4" valign="top"><strong>Pay by Credit Card</strong></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>Credit Card Number</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="x_card_num" size="24" type="text"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>Security Code</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="x_card_code" size="4" maxlength="4" type="text" width="36"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;	
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>Expiration Date</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td><select name="x_exp_date_m" size="1">'. PHP_EOL;
	$payment_form .= '						<option value="1">01</option>'. PHP_EOL;
	$payment_form .= '						<option value="2">02</option>'. PHP_EOL;
	$payment_form .= '						<option value="3">03</option>'. PHP_EOL;
	$payment_form .= '						<option value="4">04</option>'. PHP_EOL;
	$payment_form .= '						<option value="5">05</option>'. PHP_EOL;
	$payment_form .= '						<option value="6">06</option>'. PHP_EOL;
	$payment_form .= '						<option value="7">07</option>'. PHP_EOL;
	$payment_form .= '						<option value="8">08</option>'. PHP_EOL;
	$payment_form .= '						<option value="9">09</option>'. PHP_EOL;
	$payment_form .= '						<option value="10">10</option>'. PHP_EOL;
	$payment_form .= '						<option value="11">11</option>'. PHP_EOL;
	$payment_form .= '						<option value="12">12</option>'. PHP_EOL;
	$payment_form .= '					</select>/<select name="x_exp_date_y" size="1">'. PHP_EOL;
	$payment_form .= '						<option value="8">08</option>'. PHP_EOL;
	$payment_form .= '						<option value="9">09</option>'. PHP_EOL;
	$payment_form .= '						<option value="10">10</option>'. PHP_EOL;
	$payment_form .= '						<option value="11">11</option>'. PHP_EOL;
	$payment_form .= '						<option value="12">12</option>'. PHP_EOL;
	$payment_form .= '						<option value="13">13</option>'. PHP_EOL;
	$payment_form .= '						<option value="14">14</option>'. PHP_EOL;
	$payment_form .= '						<option value="15">15</option>'. PHP_EOL;
	$payment_form .= '						<option value="16">16</option>'. PHP_EOL;
	$payment_form .= '						<option value="17">17</option>'. PHP_EOL;
	$payment_form .= '						<option value="18">18</option>'. PHP_EOL;
	$payment_form .= '						<option value="19">19</option>'. PHP_EOL;
	$payment_form .= '						<option value="20">20</option>'. PHP_EOL;
	$payment_form .= '					</select></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '	<tr class="roweven">'.PHP_EOL;
	$payment_form .= '				<td rowspan="9" valign="top"><strong>Cardholder Info</strong></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;	
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>First Name</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="x_first_name" size="24" type="text"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>Last Name</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="x_last_name" size="24" type="text"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>Bill to Address<br /><br /></td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span><br /><br /></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="x_address_1" size="24" type="text"><br />'. PHP_EOL;
	$payment_form .= '					<input name="x_address_2" size="24" type="text"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>City</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="x_city" size="24" type="text"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>State</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= 						StateSelect();
	$payment_form .= '				</td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>Zip</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="x_zip" size="24" type="text"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td>Phone</td>'. PHP_EOL;
	$payment_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
	$payment_form .= '				<td>(<input name="x_phone_1" class="tel3" size="3" maxlength="3" type="text" />)  '. PHP_EOL;
	$payment_form .= '					<input name="x_phone_2" class="tel3" size="3" maxlength="3" type="text" /> -  '. PHP_EOL;
	$payment_form .= '					<input name="x_phone_3" class="tel4" size="4" maxlength="4" type="text" /></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td colspan="4" height="32">'. PHP_EOL;
	$payment_form .= '				</td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td><strong>Pay by Altiris Voucher</strong></td>'. PHP_EOL;
	$payment_form .= '				<td>Voucher Code:</td>'. PHP_EOL;
	$payment_form .= '				<td></td>'. PHP_EOL;
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="altiris_code" size="24" type="text"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td colspan="4" height="32">'. PHP_EOL;
	$payment_form .= '				</td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td><strong>Apply Redeemable</strong></td>'. PHP_EOL;
	$payment_form .= '				<td>Promotional Code:</td>'. PHP_EOL;
	$payment_form .= '				<td></td>'. PHP_EOL;	
	$payment_form .= '				<td>'. PHP_EOL;
	$payment_form .= '					<input name="promo_code" size="24" type="text"></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '			<tr class="roweven">'. PHP_EOL;
	$payment_form .= '				<td colspan="4" align="center" height="64"><input name="submit" value="Process" type="submit"/></td>'. PHP_EOL;
	$payment_form .= '			</tr>'. PHP_EOL;
	$payment_form .= '		</tbody>'. PHP_EOL;
	$payment_form .= '   		</table>'. PHP_EOL;
	$payment_form .= '	<input name="x_amount" value="'.$total.'" type="hidden" />'. PHP_EOL;
//	$payment_form .= '	<input name="x_amount" value="'.$total.'" />'. PHP_EOL;
	$payment_form .= '	</form>'. PHP_EOL;
	return $payment_form;
	}
	
	function ProcessTransaction($data = array()){

		$DEBUGGING					= 0;				# Display additional information to track down problems
		$TESTING					= 1;				# Set the testing flag so that transactions are not live
		$ERROR_RETRIES				= 2;				# Number of transactions to post if soft errors occur
		
		$auth_net_login_id			= "6zz6m5N4Et";
		$auth_net_tran_key			= "9V9wUv6Yd92t27t5";
		$auth_net_url				= "https://test.authorize.net/gateway/transact.dll";
		#  Uncomment the line ABOVE for test accounts or BELOW for live merchant accounts
		#  $auth_net_url				= "https://secure.authorize.net/gateway/transact.dll";
		
		$authnet_values				= array
		(
			"x_login"				=> $auth_net_login_id,
			"x_version"				=> "3.1",
			"x_delim_char"			=> ",",
			"x_delim_data"			=> "TRUE",
			"x_url"					=> "FALSE",
			"x_type"				=> "AUTH_CAPTURE",
			"x_method"				=> "CC",
		 	"x_tran_key"			=> $auth_net_tran_key,
		 	"x_relay_response"		=> "FALSE",
			"x_card_num"			=> "4242424242424242",
			"x_exp_date"			=> "1209",
			"x_description"			=> "Recycled Toner Cartridges",
			"x_amount"				=> "12.23",
			"x_first_name"			=> "Charles D.",
			"x_last_name"			=> "Gaulle",
			"x_address"				=> "342 N. Main Street #150",
			"x_city"				=> "Ft. Worth",
			"x_state"				=> "TX",
			"x_zip"					=> "12345",
			//"CustomerBirthMonth"	=> "Customer Birth Month: 12",
			//"CustomerBirthDay"		=> "Customer Birth Day: 1",
			//"CustomerBirthYear"		=> "Customer Birth Year: 1959",
			//"SpecialCode"			=> "Promotion: Spring Sale",
		);
		foreach($data as $key => $value){
			if(isset($authnet_values[$key]))
				$authnet_values[$key] = $value;
		}
		
		$fields = "";
		foreach( $authnet_values as $key => $value ) $fields .= "$key=" . urlencode( $value ) . "&";
		
		$ResponseString = '';
		$ResponseString .= "<hr>";
		///////////////////////////////////////////////////////////
		
		$ResponseString .= "<b>01: Post the transaction (see the code for specific information):</b><br>";
		
		
		$ch = curl_init("https://test.authorize.net/gateway/transact.dll"); 
		###  Uncomment the line ABOVE for test accounts or BELOW for live merchant accounts
		### $ch = curl_init("https://secure.authorize.net/gateway/transact.dll"); 
		curl_setopt($ch, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
		curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim( $fields, "& " )); // use HTTP POST to send form data
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. ###
		$resp = curl_exec($ch); //execute post and get results
		curl_close ($ch);
		return array('output'=> $ResponseString, 'response'=> $resp);
	}
	
	
	function DisplayAdditionalAttendees(){
		if(isset($_SESSION['cart']['customer_id'])){
			$this_Session = $_COOKIE['MySession'];			
			$url_cart = "https://". $_SERVER['SERVER_NAME'] . "/freeway/custom/webservices/index.php?service=ShoppingCart";
			$Cart = new SoapClient($url_cart);
			$Cart->__setCookie('ws_freeway', $this_Session);
			$Cart->__setCookie('customer_id', $_SESSION['cart']['customer_id']);
			$cart_contents = $Cart->getCustomerCart();
			$output = $cart_contents;
			unset($Cart);
		}
		else{
			$output = "LOGIN...";
		}
		if($output != "LOGIN..."){
			$attendee_form = "";	
			$a = 0;
			$b = 0;	

			foreach($output[cart] as $event){
				$attendee_form .= '<input name="session_id['.++$a.']" value="'.$event[sessions_id].'" type="hidden" />';
				$attendee_form .= '<input name="event_id['.$a.']" value="'.$event[events_id].'" type="hidden" />';
				$attendee_form .= '		<tr class="roweven">'.PHP_EOL;
				$attendee_form .= '			<td rowspan="'.(int)5*$event[qnty].'" valign="top"><strong>'.$event[events_categories_heading_title].' <br /> '.str_replace(', ', ' to ',$event[events_description]).'</strong></td>'. PHP_EOL;
				$attendee_form .= '		</tr>'. PHP_EOL;
				for($i = 0; $i < $event[qnty]; $i++){
					$attendee_form .= '			<tr class="roweven">'. PHP_EOL;
					$attendee_form .= '				<td>First Name</td>'. PHP_EOL;
					$attendee_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
					$attendee_form .= '				<td>'. PHP_EOL;
					$attendee_form .= '					<input name="First Name['.$a.']['.++$b.']" size="24" type="text"></td>'. PHP_EOL;
					$attendee_form .= '			</tr>'. PHP_EOL;
					$attendee_form .= '			<tr class="roweven">'. PHP_EOL;
					$attendee_form .= '				<td>Last Name</td>'. PHP_EOL;
					$attendee_form .= '				<td align="right"><span id="req">*</span></td>'. PHP_EOL;
					$attendee_form .= '				<td>'. PHP_EOL;
					$attendee_form .= '					<input name="Last Name['.$a.']['.$b.']" size="24" type="text"></td>'. PHP_EOL;
					$attendee_form .= '			</tr>'. PHP_EOL;
					$attendee_form .= '			<tr class="roweven">'. PHP_EOL;
					$attendee_form .= '				<td>E-mail</td>'. PHP_EOL;
					$attendee_form .= '				<td align="right"><span id="req"></span></td>'. PHP_EOL;
					$attendee_form .= '				<td>'. PHP_EOL;
					$attendee_form .= '					<input name="email['.$a.']['.$b.']" size="24" type="text"></td>'. PHP_EOL;
					$attendee_form .= '			</tr>'. PHP_EOL;
					$attendee_form .= '			<tr class="roweven">'. PHP_EOL;
					$attendee_form .= '				<td>Phone</td>'. PHP_EOL;
					$attendee_form .= '				<td align="right"><span id="req"></span></td>'. PHP_EOL;
					$attendee_form .= '				<td>(<input name="phone_1['.$a.']['.$b.']" class="tel3" size="3" maxlength="3" type="text"> )  '. PHP_EOL;
					$attendee_form .= '					<input name="phone_2['.$a.']['.$b.']" class="tel3" size="3" maxlength="3" type="text"> -  '. PHP_EOL;
					$attendee_form .= '					<input name="phone_3['.$a.']['.$b.']" class="tel4" size="4" maxlength="4" type="text"></td>'. PHP_EOL;
					$attendee_form .= '			</tr>'. PHP_EOL;
					$attendee_form .= '			<tr class="roweven">'. PHP_EOL;
					$attendee_form .= '				<td colspan="4" height="32">'. PHP_EOL;
					$attendee_form .= '				</td>'. PHP_EOL;
					$attendee_form .= '			</tr>'. PHP_EOL;
					//$attendee_form .= '   		<input name="required['.$a.']['.$b.']" type="hidden" value="First Name['.$a.']['.$b.'],Last Name['.$a.']['.$b.']" />'. PHP_EOL;

				}
			}						
			$attendee_form_head =  "<form action=\"https://eden.iwsdev.com/process-order\" method=\"post\" name=\"payment\">" .PHP_EOL;
			$attendee_form_head .= '<input name="type" type="hidden" value="attendees" />';
			$attendee_form_head.= '	<table class="data" border="0" cellpadding="0" cellspacing="0">'. PHP_EOL;
			$attendee_form_head.= '		<tbody><tr class="roweven">'. PHP_EOL;
			$attendee_form_head.= '			<th colspan="5">Attendees</th>'. PHP_EOL;
			$attendee_form_head.= '		</tr>'. PHP_EOL;
//			$attendee_form_foot  = '   		<input name="required['.$a.']" type="hidden" value="' '" />'. PHP_EOL;
			$attendee_form_foot = '			<tr class="roweven">'. PHP_EOL;
			$attendee_form_foot .= '				<td colspan="4" align="center" height="64"><input name="submit" value="Submit" type="submit"></td>'. PHP_EOL;
			$attendee_form_foot .= '			</tr>'. PHP_EOL;
			$attendee_form_foot .= '		</tbody>'. PHP_EOL;
			$attendee_form_foot .= '   </table></form>'. PHP_EOL;
			$output = $attendee_form_head. $attendee_form . $attendee_form_foot;
		}
		return $output;		
	}

	if(isset($_POST[type]) && $_POST[type] == "order"){	
		$_SESSION[order] = $_POST;
		$form_corrections = DisplayOrderForm();
		$form_vars = FormCheck();
		if(!isset($form_vars[0])){
				$step2 = DisplayAdditionalAttendees();
				echo $step2;
				//print_r(ProcessTransaction($form_vars));
		}
		else{
			foreach($_POST as $key => $value){
				//  Check for any errors
				if(array_key_exists($key, $form_vars)){
					$message = $form_vars[$key];
					//  Attached the error message to the form 
					if($key != "x_phone_1" && $key != "x_phone_2" && $key != "x_phone_3")
						$form_corrections =str_replace("<input name=\"$key\"", "<em style=\"color: #B54639\">$message</em><br />"."<input name=\"$key\" value=\"$value\" ",$form_corrections);
					//  This special case needs different formatting
					elseif($key == "x_phone_1" || $key == "x_phone_2" || $key == "x_phone_3"){
						$form_corrections =str_replace(	"(<input name=\"x_phone","<em style=\"color: #B54639\">$message</em><br />"."(<input name=\"x_phone",$form_corrections);
						$form_corrections = str_replace("name=\"$key\"", "name=\"$key\" value=\"$value\" ", $form_corrections);						
					}
				}			
				else{
					//  If no error was found attached the submitted value to the form input
					$form_corrections = str_replace("name=\"$key\"", "name=\"$key\" value=\"$value\" ", $form_corrections);
					// also select any options that were selected... 
					$form_corrections = str_replace("<option value=\"$value\">", "<option value=\"$value\" selected>", $form_corrections);
				}					
			}
			echo $form_corrections;
		}
	}	
	elseif(isset($_POST[type]) && $_POST[type] == "attendees"){
		$form_corrections = DisplayAdditionalAttendees();
		$errors = array();
		$values = array();
		foreach($_POST as $key => $form_array){
			if($key == "First_Name" || $key == "Last_Name"){
				if(is_array($form_array))
					foreach($form_array as $form_array_key => $value){
						foreach($value as $field_key => $field){
							if($field == ""){
								//echo $key;
								$errors[$key][$form_array_key][$field_key] = "Please enter a ".str_replace('_', ' ', $key);
							}//end if
							else{
								$values[$key][$form_array_key][$field_key] = $field;
							}//end else
						}//end foreach $value
					}// end foreach $form_array
				}//if is_array()
			else{
				$values[$key] = $form_array;
			}
		}//end foreach $_POST
		if(count($errors)>1){
			foreach($errors as $key => $value){
				foreach($value as $key2 => $value2)
					foreach($value2 as $key3 => $value3){
						$search = "<input name=\"".str_replace('_', ' ',$key)."[$key2]"."[$key3]" . "\"";
						$form_corrections = str_replace($search, "<em style=\"color: red\">$value3</em><br />$search", $form_corrections);
					}			
			}
			foreach($values as $key => $value){
				if(is_array($value))
					foreach($value as $key2 => $value2)
						if(is_array($value2))
							foreach($value2 as $key3 => $value3){
								$newKey = (stripos($key, 'phone') !== false) ? $key : str_replace('_', ' ',$key);
								$search = "<input name=\"".$newKey."[$key2]"."[$key3]" . "\"";
								$form_corrections = str_replace($search, $search ." value=\"$value3\"", $form_corrections);
							}//end if is_array()
						else{
							$search = "<input name=\"".str_replace('_', ' ',$key)."[$key2]"."\"";
							$form_corrections = str_replace($search, $search ." value=\"$value2\"", $form_corrections);
						}//end else
				else{
					$search = "<input name=\"".str_replace('_', ' ',$key)."\"";
					$form_corrections = str_replace($search, $search ." value=\"$value\"", $form_corrections);
				}//end else
			}//end foreach $value
			echo $form_corrections;
		}//end if count($errors)
		else{	
			//$sessions_id, $events_id, $customers_id, $customers_firstname, $customers_lastname, $customers_telephone, $customers_emailaddress, $customers_query, $customers_attend, $customers_paid, $customers_paid_type, $final_price, $paid
			$url_cart = "https://". $_SERVER['SERVER_NAME'] . "/freeway/custom/webservices/index.php?service=ShoppingCart";
			$Cart = new SoapClient($url_cart);
			$Cart->__setCookie('ws_freeway', $this_Session);
			$Cart->__setCookie('customer_id', $_SESSION['cart']['customer_id']);
			$url_attend = "https://". $_SERVER['SERVER_NAME'] . "/freeway/custom/webservices/index.php?service=Customer";
			echo $url_attend;
			$Attendee = new SoapClient($url_attend);
			$Attendee->__setCookie('ws_freeway', $this_Session);
			$Attendee->__setCookie('customer_id', $_SESSION['cart']['customer_id']);
			echo $_SESSION['cart']['customer_id'];
			$attendee_sessions = $_POST[session_id];
			$attendee_events = $_POST[event_id];
			$address = '';$address2 = '';$city= '';$state = '';$zip = '';$email2 = '';$dob = '';$gender = '';$company = '';$fax = '';$phone2 = '';
			$customers_query= ''; $customers_attend= ''; $customers_paid= 'Y'; $customers_paid_type= ''; $final_price= '';$paid = 'Y'; 
			foreach($_POST[First_Name] as $event => $attendees){
				foreach($attendees as $name_key => $first_name){
					$password = chr(mt_rand(65,90)).chr(mt_rand(97,122)).chr(mt_rand(97,122)).chr(mt_rand(97,122)).chr(mt_rand(35,38)).chr(mt_rand(65,90)).mt_rand(1001,9999);
					$customers_firstname = $first_name;
					$last_name = $_POST[Last_Name][$event][$name_key];
					$customers_lastname = $last_name;
					$email = $_POST[email][$event][$name_key];
					$customers_emailaddress = $email;
					$phone = $_POST[phone_1][$event][$name_key] . $_POST[phone_2][$event][$name_key] . $_POST[phone_3][$event][$name_key];
					$customers_telephone = $phone;
					$sessions_id = $attendee_sessions[$event];
					$events_id = $attendee_events[$event];
					//echo "$first_name, $last_name, $address, $address2, $city, $state, $zip, $email, $email2, $dob, $gender, $phone, $company, $fax, $phone2, $password<br />";
					$customers = $Attendee->AddCustomer($first_name, $last_name, $address, $address2, $city, $state, $zip, $email, $email2, $dob, $gender, $phone, $company, $fax, $phone2, $password);
					$customers_id = $customers[0];
					//echo "$sessions_id, $events_id, $customers_id, $customers_firstname, $customers_lastname, $customers_telephone, $customers_emailaddress, $customers_query, $customers_attend, $customers_paid, $customers_paid_type, $final_price, $paid<br />";
					$someVar[] = $Cart->getCustomerCart();
					$some = $Cart->AttendeeAdd((int)$sessions_id, (int)$events_id, (int)$customers_id, $customers_firstname, $customers_lastname, $customers_telephone, $customers_emailaddress, $customers_query, $customers_attend, $customers_paid, $customers_paid_type, $final_price, $paid);
					echo $some. "<br />";
				}
			//$Attendee->AddCustomer($first_name, $last_name, $address, $address2, $city, $state, $zip, $email, $email2, $dob, $gender, $phone, $company, $fax, $phone2, $password);
			
			}
			//$output = $cart_contents;

		}//end else (no errors...)
	}//end elseif attendees
	
?>